package com.stateofflow.invariantj;

import java.lang.reflect.Method;

public class Utils {
    public static final String INSERTED_MEMBERS_NAME_PREFIX = "invariantj$";
    public static final Object[] NULL_OBJECT_ARRAY = new Object[] { null };
    
    private static final Class[] SUPERCLASS_CHECK_OWN_INVARIANT_METHOD_PARAMETER_TYPES = new Class[] { Throwable.class };

    private Utils() {
        // No-one should construct
    }
    
    public static String getCheckOwnInvariantMethodName(Class forClass) {
        String className = forClass.getName();
        return getCheckOwnInvariantMethodName(className);
    }

    public static String getCheckOwnInvariantMethodName(String className) {
        return Utils.INSERTED_MEMBERS_NAME_PREFIX + className.replace('.' , '_') + "$checkInvariant";
    }
    
    public static Method getSuperclassCheckOwnInvariantMethodIfExists(Class forClass) {
        Class superclass = forClass.getSuperclass();
        while (superclass != null) {
            try {
                return superclass.getMethod(getCheckOwnInvariantMethodName(superclass), SUPERCLASS_CHECK_OWN_INVARIANT_METHOD_PARAMETER_TYPES);
            } catch (SecurityException e) {
                throw e;
            } catch (NoSuchMethodException e) {
                // Try the next superclass
            }
            superclass = superclass.getSuperclass();
        }
        return null;
    }
}
